package top.jidanmin.demo.lightserver.device;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class DeviceService {
    private DeviceRepository deviceRepository;

    public DeviceService(DeviceRepository deviceRepository) {
        this.deviceRepository = deviceRepository;
    }

    public Device addDevice(Device device) {
        DeviceEntity entity = new DeviceEntity(device.getName());

        entity = deviceRepository.save(entity);

        return new Device(entity.getId(), entity.getName());
    }

    public List<Device> findAll() {
        List<DeviceEntity> entities = deviceRepository.findAll();
        List<Device> devices = new ArrayList<>();

        entities.forEach(entity -> devices.add(new Device(entity)));
        
        return devices;
    }
}
